package com.example.demo.controller;

import com.example.demo.common.entity.MsgInfo;

import java.sql.Connection;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import com.example.demo.entity.websocket.Message;
import com.example.demo.entity.websocket.MessageForm;
import com.example.demo.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/message")
public class MessageController {
    //逻辑层
    @Resource
    private MessageService messageService;

    // 查找未读数量
    @GetMapping("/findNoReadMessageLength")
    public MsgInfo findNoReadMessage(@RequestParam("userId") String userId) {
            Integer total = messageService.findNoReadMessageLength(userId);
            return MsgInfo.ok(total);
    }

    // 查找两个人的聊天记录
    @PostMapping("/findMessageBySendUserAndReceiveUser")
    public MsgInfo findMessageBySendUserAndReceiveUser(@RequestParam("sendUserId") String sendUserId,
                                                        @RequestParam("receiveUserId") String receiveUserId) {
            return MsgInfo.ok(messageService.findMessageBySendUserAndReceiveUser(sendUserId, receiveUserId));
    }
    // 发送信息
    @PostMapping("/sendMessage")
    public MsgInfo sendMessage(@RequestBody Message message) {
            messageService.sendMessage(message);
            return MsgInfo.ok("发送成功!");
    }

//    // 查找我的用户信息数据
    @PostMapping("searchUserForForm")
    public MsgInfo searchUserForForm(@RequestParam("loginUserId") String loginUserId, @RequestParam("searchUserName") String searchUserName) {
            List<MessageForm> messages = messageService.searchUserForForm(loginUserId, searchUserName);
            return MsgInfo.ok(messages);
    }
}


